/*
 * Created on 2005-8-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.store;

import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ArticleCateMgr extends BaseObj {
	public ArticleCateMgr() {}
	public ArticleCategory get(String cateId) {
		clearErr();
		ArticleCategory cate = new ArticleCategory(cateId);
		DbConn conn = new DbConn();
		try {
			String sql = "select * from t_article_category where category_id = ?";
			conn.prepare(sql);
			conn.setString(1,cateId);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			cate.setCategoryName(get(rs,0,"category_name"));
			cate.setParentId(get(rs,0,"parent_id"));
			cate.setCategoryId(get(rs,0,"category_id"));
			cate.setOrderId(getInt(rs,0,"order_id"));
			if (getInt(rs,0,"is_leaf") == 1)
				cate.setLeaf();
			cate.setCategoryDesc(get(rs,0,"category_desc"));
			return cate;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "select * from t_article_category where 1=1 ";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				ArticleCategory cate = new ArticleCategory(get(rs,i,"category_id"));
				cate.setCategoryName(get(rs,i,"category_name"));
				cate.setParentId(get(rs,i,"parent_id"));
				cate.setCategoryId(get(rs,i,"category_id"));
				cate.setOrderId(getInt(rs,i,"order_id"));
				if (getInt(rs,i,"is_leaf") == 1)
					cate.setLeaf();
				cate.setCategoryDesc(get(rs,i,"category_desc"));
				r.add(cate);
			}
			return r;
		}
		return null;
	}
}
